<?php 
	 require('config.php');
	 
	 function getConnection($user, $pass, $mysqladd, $dbname) {
		//CONNECT TO MYSQL
		$link=mysql_connect($mysqladd,$user,$pass) or die('<Error>Database Error: ' . mysql_error() . '</Error>');
		//CONNECT TO DATABASE
		mysql_select_db($dbname, $link) or die('<Error>Could not connect to DB: ' . mysql_error() . '</Error>');
		return $link;			
	}
	 
	 $conn = getConnection($mysqluser, $mysqlpass, $mysqladd, $databasename);
	 
	 $uploadername = $_GET['uploadername'];
	 $roomguid = $_GET['roomguid'];
	 $fname = $_GET['fname'];
	 $fcompletename = $_GET['fcompletename'];
	 $fnote = $_GET['note'];
	 $type = $_GET['type'];
	
	 $dirname = '../djData/'.$roomguid;
	 if(!file_exists($dirname))
		mkdir($dirname);
	 
	if($type == 0)
		$dirname .= '/saveattachment';
	else{
		$desiredfilename = $_GET['desiredfilename'];
		$dirname .= '/savemessageattachment';
	}
	 
	 if(!file_exists($dirname))
		mkdir($dirname);
		
	 $target = $dirname."/"; 
	 
	 if($type == 0)
		$target = $target . basename( $_FILES['Filedata']['name']) ; 
	 else 
		$target = $target.$desiredfilename; 
		
	 if(move_uploaded_file($_FILES['Filedata']['tmp_name'], $target)) 
	 {
		echo "The file ". basename( $_FILES['Filedata']['name']). " has been uploaded";
		$now= date( 'Y-m-d H:i:s', time());
		
		if($type == 0)
			mysql_query("INSERT INTO attachment(att_filename, att_room_guid, att_teachername, att_date, att_note) VALUES('" . $fcompletename . "','" . $roomguid . "','" . $uploadername . "','" . $now . "','" . $fnote . "')");
	 } 
	 else {
		echo "Sorry, there was a problem uploading your file.";
	 }
	 
	 mysql_close($conn);
 ?> 